package com.konne.nightmare.FastPublicOpinion.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/1/26  Time:下午 3:20.
 * description: ** .
 */
public class QJAlertDialog extends Dialog {
    public QJAlertDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        dialogWindow.getDecorView().setPadding(SizeUtils.dp2px(20), 0, SizeUtils.dp2px(20),0);
        dialogWindow.setGravity(Gravity.CENTER);
    }
    public static class Builder{
        private Context context;
        private QJAlertDialog dialog;
        private View contentView;
        public Builder(Context context) {
            this.context = context;
        }
        private String title;
        private String positive="确定";
        private String negative="取消";
        private DialogInterface.OnClickListener positiveClick;
        private DialogInterface.OnClickListener negativeClick;
        private CharSequence msg;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMsg(CharSequence msg) {
            this.msg = msg;
            return this;
        }
        public Builder setPositiveButton(String txt,DialogInterface.OnClickListener clickListener){
            if(txt!=null){
                positive=txt;
            }
            positiveClick=clickListener;
            return this;
        }
        public Builder setNegativeButton(String txt,DialogInterface.OnClickListener clickListener){
            if(txt!=null){
                negative=txt;
            }
            negativeClick=clickListener;
            return this;
        }
        public QJAlertDialog builder(){
            dialog=new QJAlertDialog(context);
            initView();
            dialog.setContentView(contentView);
            return dialog;
        }
        public void show(){
            builder().show();
        }
        private void initView(){
            contentView= LayoutInflater.from(context).inflate(R.layout.dialog_qj_dialog,null);
            if(!StringUtils.isEmpty(title)){
                ((TextView)contentView.findViewById(R.id.tv_title)).setText(title);
            }
            if(!StringUtils.isEmpty(msg)){
                ((TextView)contentView.findViewById(R.id.tv_msg)).setText(msg);
            }
            TextView stv_cancel=contentView.findViewById(R.id.stv_cancel);
            stv_cancel.setText(negative);
            stv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(negativeClick!=null){
                        negativeClick.onClick(dialog,0);
                    }
                    dialog.dismiss();
                }
            });
            TextView stv_submit=contentView.findViewById(R.id.stv_submit);
            stv_submit.setText(positive);
            stv_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(positiveClick!=null){
                        positiveClick.onClick(dialog,0);
                    }
                    dialog.dismiss();
                }
            });
        }
    }
}
